package com.tanhua.server.service;



import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;

import com.tanhua.server.interceptor.UserTokenInterceptor;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class TodayBestService {
    @Autowired
    private UserService userService;
    @Autowired
    private RecommendUserService recommendUserService;
    @Value("${tanhua.sso.default.user}")
    private Long defaultUser;
    @Autowired
    private UserInfoService userInfoService;
    //查询今日佳人
    public TodayBest queryUserByToken() {
        //获取user对象
        User user = UserThreadLocal.get();
        if (null==user){
            return null;
        }
        //获取今日佳人
        TodayBest todayBest = this.recommendUserService.queryTodayBast(user.getId());//存在佳人会带着分数跟ID返回
        if (null==todayBest){
            //没有佳人就给系统默认的信息
            todayBest = new TodayBest();
            todayBest.setId(defaultUser);
            todayBest.setFateValue(85L);//固定值，缘分值
        }
        //补全其他信息，用userinfo表获取
        UserInfo userInfo = this.userInfoService.queryById(todayBest.getId());
        if (null==userInfo){
            return null;
        }
        todayBest.setNickname(userInfo.getNickName());//姓名
        todayBest.setAge(userInfo.getAge());
        todayBest.setTags(StringUtils.split(userInfo.getTags(),","));
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        todayBest.setAvatar(userInfo.getLogo());//头像
        return todayBest;

    }
    //获取推荐列表
    public PageResult queryRecommendation(RecommendUserQueryParam queryParam) {
        //获取user对象
        User user = UserThreadLocal.get();
        if (null==user) {
            return null;
        }
        //创建结果集
        PageResult pageResult= new PageResult();
        pageResult.setPage(queryParam.getPage());
        pageResult.setPagesize(queryParam.getPagesize());
        //获取该用户的推荐表，包含推荐人分数id等
        PageInfo<RecommendUser> pageInfo = this.recommendUserService.
                queryRecommendUserList(user.getId(),queryParam.getPage(),queryParam.getPagesize());
        List<RecommendUser> records = pageInfo.getRecords();
        for (RecommendUser record : records) {
            if (CollectionUtils.isEmpty(records)){
                //为空查不到推荐信息
                return pageResult;
            }
        }
        //填充个人信息
        //收集所有用户的id
        List<Object> users = new ArrayList<>();
        for (RecommendUser record : records) {//遍历获取当前用户推荐人所有ID集
            users.add(record.getUserId());
        }

        List<UserInfo> userInfoList= this.userInfoService.queryUserInfoList(users);
       if (CollectionUtils.isEmpty(userInfoList)){
           return pageResult;//按条件查不到满足条件的推荐人
       }
       //创建推荐人列表集合
        List<TodayBest> todayBestList = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            TodayBest todayBest = new TodayBest();
            todayBest.setAge(userInfo.getAge());//年龄
            todayBest.setAvatar(userInfo.getLogo());//头像
            todayBest.setId(userInfo.getUserId());// id
            todayBest.setNickname(userInfo.getNickName());//昵称
            todayBest.setTags(StringUtils.split(userInfo.getTags(),","));
            todayBest.setGender(userInfo.getSex().getValue()==1?"man":"woman");//性别
            for (RecommendUser record : records) {
                if (userInfo.getUserId().longValue()==record.getUserId().longValue()){
                    double score = Math.floor(record.getScore());
                    todayBest.setFateValue(Double.valueOf(score).longValue());
                    break;
                }
            }
            todayBestList.add(todayBest);
        } //推荐列表信息获取完毕
        //按照缘分值排序
        System.out.println(todayBestList.size());
        Collections.sort(todayBestList, new Comparator<TodayBest>() {
            @Override
            public int compare(TodayBest o1, TodayBest o2) {
               return o2.getFateValue().intValue() - o1.getFateValue().intValue();
            }
        });
        pageResult.setItems(todayBestList);
        return pageResult;
    }
}
